﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

//Alon
namespace InformationExtraction 
{
    class BirthdayProcess : ExtractorProcess
    {
        public static string[] months = {"January", "February", "March", "April", "May", "June", 
                              "July", "August", "September", "October", "November", "December"};

        /// <param name="str">plain text string</param>
        public static DateTime[] GetDates(string str)
        {
            List<DateTime> dates = new List<DateTime>();

            //option 1
            Regex bornIn = new Regex("(" + string.Join("|", months) + @") \d{1,2} (, )*\d{4}");
            var matches = bornIn.Matches(str);
            foreach (Match match in matches)
            {
                string[] date = match.Value.Replace(", ", "").Split(' ');
                dates.Add(new DateTime(int.Parse(date[2]), Array.IndexOf(months, date[0], 0) + 1, int.Parse(date[1])));
            }

            //option 2
            bornIn = new Regex(@"\d{1,2} (" + string.Join("|", months) + @") \d{4}");
            matches = bornIn.Matches(str);
            foreach (Match match in matches)
            {
                string[] date = match.Value.Split(' ');
                dates.Add(new DateTime(int.Parse(date[2]), Array.IndexOf(months, date[1], 0) + 1, int.Parse(date[0])));
            }

            return dates.ToArray();
        }

        /// <param name="str">plain text string</param>
        public static DateTime[] GetDatesInBracket(string str)
        {            
            //find the part between the bracket            
            Regex pat = new Regex(@"-LRB- (\w|,|-| )* -RRB-");
            string inBracket = pat.Match(str).Value;

            if (string.IsNullOrWhiteSpace(inBracket))
                return new DateTime[0];

            inBracket = inBracket.Substring(6, inBracket.Length - 12);
            return GetDates(inBracket);            
        }

        public override void Execute(Person person, Page page)
        {
            DateTime[] dates = GetDatesInBracket(page.Sentences[0].PlainText);

            if (dates.Length > 0)
                person.Birthday = dates[0];
        }
    }
}
